Next.js template
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

[dataId].js 837B

123456789101112131415161718192021222324252627282930313233343536
  1. const Person = require('../../../models/person');
  2. import dbConnect from '../../../utils/helpers/dbHelpers';
  3. async function handler(req, res) {
  4. const { method } = req;
  5. await dbConnect();
  6. switch (method) {
  7. case 'GET': {
  8. try {
  9. const dataId = req.query.dataId;
  10. const person = await Person.findOne({ customID: dataId });
  11. console.log(person);
  12. if (!person) {
  13. throw new Error('Person with this id does not exist!');
  14. }
  15. res.status(200).json({
  16. message: 'Fetch single data successfull!',
  17. singleData: person,
  18. });
  19. } catch (error) {
  20. res.status(400).json({ message: error.message });
  21. }
  22. break;
  23. }
  24. default:
  25. res.status(405).json({ message: 'Method not allowed' });
  26. break;
  27. }
  28. }
  29. export default handler;